package com.stars.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.stars.RestPage;
import com.stars.code.Code;
import com.stars.mapper.OrgFilesPerdomMapper;
import com.stars.mapper.OrgPerdomMapper;
import com.stars.pojo.OrgFilesPerdom;
import com.stars.pojo.OrgPerdom;
import com.stars.service.OrgPerdomService;
import com.stars.utils.FastDFSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author 86185
* @description 针对表【org_perdom】的数据库操作Service实现
* @createDate 2024-11-13 13:57:11
*/
@Service
public class OrgPerdomServiceImpl extends ServiceImpl<OrgPerdomMapper, OrgPerdom>
    implements OrgPerdomService{



    @Autowired
    private   OrgPerdomMapper orgPerdomMapper;

    @Autowired
    private OrgFilesPerdomMapper orgFilesPerdomMapper ;


    //  对当前的人事文档进行修改
    @Transactional
    @Override
    public Integer updatePerdom(MultipartFile file, String jsonData)throws Exception {

        OrgPerdom orgPerdom = null;
        try {
            if (jsonData != null){
                orgPerdom = new ObjectMapper().readValue(jsonData, OrgPerdom.class);
                this.orgPerdomMapper.updateById(orgPerdom);
            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        System.out.println(file == null);
        //上传图片 保存文件
         if (file == null){
             return  Code.PERSONNEL_UPDATE_ISNULL ;
         }

        String[] strings = FastDFSUtils.uploadFile(file.getInputStream(),
                file.getOriginalFilename() , "size",  file.getSize()+"" );
        System.out.println("192.168.19.132:8888/"+strings[0]+"/"+strings[1]);



//         保存文件路径
            if (strings == null){
                return Code.PERSONNEL_UPDATE_ISNULL ;
            }

            OrgFilesPerdom orgFilesPerdom = new OrgFilesPerdom();
            orgFilesPerdom.setRemoteName(strings[0]);
            orgFilesPerdom.setGroupName(strings[1]);
            orgFilesPerdom.setFileName(file.getOriginalFilename());
            orgFilesPerdom.setPerdomid(orgPerdom.getId());
            orgFilesPerdom.setUrl("192.168.19.132:8888/"+strings[0]+"/"+strings[1]);
            orgFilesPerdomMapper.insert(orgFilesPerdom);

        return  Code.PERSONNEL_UPDATE_Ok ;
    }

    // 多条件查询人事文档
    @Override
    public  Page<OrgPerdom> selectPerdom(Map<String, Object> map) {
        QueryWrapper<OrgPerdom> orgPerdomQueryWrapper = new QueryWrapper<>();
          Integer pageNum = 1 ;
          Integer pageSIze = 5 ;
        if (map == null){
            this.orgPerdomMapper.selectPage(new Page<>(pageNum,pageSIze),null);
         }
        pageNum =  map.get("pageNum") == null ?  1 :   Integer.valueOf(map.get("pageNum")+"");
        pageSIze =  map.get("pageSIze") == null ?  5 :   Integer.valueOf(map.get("pageSIze")+"");

        String name = map.get("name")+"";
        if (!"null".equals(name) ){
            orgPerdomQueryWrapper.likeRight("NAME",name);
        }
        String createdate = map.get("createdate")+"";
           if (!"null".equals(createdate)){
               orgPerdomQueryWrapper.ge("CREATEDATE",createdate);
           }
           String domstate =   map.get("domstate")+"";

            if ( !"null".equals(domstate)){
                orgPerdomQueryWrapper.eq("DOMSTATE",domstate);
            }

        Page<OrgPerdom> orgPerdomPage =
                this.orgPerdomMapper.selectPage(new Page<>(pageNum, pageSIze), orgPerdomQueryWrapper);

        return  orgPerdomPage;
    }
}




